﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace BookStore
{
    internal class sqlDataAdapter
    {
        //此方法加载全部书籍记录
        public void LoadDataToDataGridView(string query ,DataGridView dataGridView,SqlConnection sqlConnection)
        {
            using (SqlDataAdapter Adapter = new SqlDataAdapter(query, sqlConnection))
            {
                    DataTable DataTable = new DataTable();
                    sqlConnection.Open();
                    Adapter.Fill(DataTable);
                    dataGridView.DataSource = DataTable;
                    sqlConnection.Close();
            }
        }

        //overload 此方法用于加载符合筛选条件的书籍记录
        public void LoadDataToDataGridView(string query, SqlParameter sqlParameters ,DataGridView dataGridView, SqlConnection sqlConnection)
        {
            using (SqlCommand command = new SqlCommand(query, sqlConnection))
            {
                    command.Parameters.Add(sqlParameters);
                    SqlDataAdapter Adapter= new SqlDataAdapter(command);
                    DataTable DataTable = new DataTable();
                    sqlConnection.Open();
                    Adapter.Fill(DataTable);
                    dataGridView.DataSource = DataTable;
                    sqlConnection.Close();
            }
        }
    }
}
